keep cvrnrT aar
duplicates drop
expand 4
bysort cvrnrT aar: gen d_yr = _n - 4
gen aarA = aar + d_yr
*gen p = (d_yr>0)

rename cvrnrT cvrnrA
merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_com_all_cvrnr.dta", keep(match master) keepusing(nprod_vars vrdA_vars) nogen
rename vrdA_vars vrdT_vars

merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_dom_all_cvrnr_dro.dta", keep(match master) keepusing(nprod vrdA) nogen
rename vrdA_dom_dro vrdT_dom
rename nprod_dro nprod_dom

merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_exports_all_cvrnr.dta", keep(match master) keepusing(nprod nmark vrdA) nogen	
rename vrdA vrdT_exp
rename nprod nprod_exp
rename nmark nmark_exp

rename cvrnrA cvrnr
rename aar aar_ori
rename aarA aar
merge m:1 cvrnr aar using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_firm_subset1865.dta", keep(match) keepusing(employee_cvrnr) nogen
rename cvrnr cvrnrT
rename aar aarA
rename aar_ori aar

*In any year
gen multiproduct_vars = (nprod_vars>1)
replace multiproduct_vars = . if nprod_vars==.

*In any year
gen multiproduct_dom = (nprod_dom>1)
replace multiproduct_dom = . if nprod_dom==.

*In any year, conditional on exporting
gen multiproduct_exp_con = (nprod_exp>1)
replace multiproduct_exp_con = . if nprod_exp==.

*In any year
gen multiproduct_exp = (nprod_exp>1)
replace multiproduct_exp = 0 if nprod_exp==.

*Replace missing with zeros
gen nprod_exp0 = nprod_exp
replace nprod_exp0 = 0 if nprod_exp==.

*In year of merger
gen nprod_vars_aar = nprod_vars if aar==aarA
gen nprod_dom_aar = nprod_dom if aar==aarA
gen nprod_exp_aar = nprod_exp if aar==aarA

collapse (mean) nprod_vars nprod_dom nprod_exp nprod_exp0 nmark_exp vrdT_vars vrdT_dom vrdT_exp employee_cvrnr nprod_vars_aar nprod_dom_aar nprod_exp_aar (max) multiproduct_vars multiproduct_dom multiproduct_exp multiproduct_exp_con, by(cvrnrT aar)

gen exporter = (nprod_exp>0 & nprod_exp!=.)

*Same as multiproduct_dom
gen multiproduct_vars_avg = (nprod_vars>1)
replace multiproduct_vars_avg = . if nprod_vars==.

*Same as multiproduct_dom
gen multiproduct_dom_avg = (nprod_dom>1)
replace multiproduct_dom_avg = . if nprod_dom==.

*Different from multiproduct_exp
gen multiproduct_exp_avg = (nprod_exp0>1)

*Same as multiproduct_exp_con
gen multiproduct_exp_con_avg = (nprod_exp>1)
replace multiproduct_exp_con_avg = . if nprod_exp==.

gen exporter_aar = (nprod_exp_aar>0 & nprod_exp_aar!=.)

gen nprod_vars5 = (nprod_vars>=5 & nprod_vars!=.)
gen nprod_dom5 = (nprod_dom>=5 & nprod_dom!=.)
gen nprod_exp5 = (nprod_exp>=5 & nprod_exp!=.)

*Targets
* Fact 1
*Share of multiproduct firms on ALL markets
*tab multiproduct_vars
sum multiproduct_vars
post stats ("Fact 1") ("Targets") ("Multiproduct") (r(N)) (r(mean)) (r(sd))
*Same
tab multiproduct_vars_avg

*Share of firms with 5 or more products on ALL markets
sum nprod_vars5 if nprod_vars>0 & nprod_vars!=.
post stats ("Fact 1") ("Targets") ("5 or more") (r(N)) (r(mean)) (r(sd))
